package com.pet.shop.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class YsDeviceApi {

    private static final String API_URL = "https://open.ys7.com";

    /**
     * 添加设备到账号下
     * @param accessToken
     * @param deviceSerial
     * @param validateCode
     * @return
     */
    public static YsApiResult addDevice(String accessToken, String deviceSerial, String validateCode){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("accessToken", accessToken);
        paramMap.put("deviceSerial", deviceSerial);
        paramMap.put("validateCode", validateCode);
        String result = HttpsClientUtil.doPost(API_URL + "/api/lapp/device/add", paramMap);
        return JSONObject.parseObject(result, YsApiResult.class);
    }

    /**
     * 删除设备
     * @param accessToken
     * @param deviceSerial
     * @return
     */
    public static YsApiResult deleteDevice(String accessToken, String deviceSerial){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("accessToken", accessToken);
        paramMap.put("deviceSerial", deviceSerial);
        String result = HttpsClientUtil.doPost(API_URL + "/api/lapp/device/add", paramMap);
        return JSONObject.parseObject(result, YsApiResult.class);
    }

    /**
     * 获取指定有效期的直播地址
     * @param accessToken
     * @param deviceSerial
     * @return
     */
    public static JSONObject getLappLiveAddress(String accessToken, String deviceSerial){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("accessToken", accessToken);
        paramMap.put("deviceSerial", deviceSerial);
        paramMap.put("channelNo", "1");
        paramMap.put("expireTime", "");
        String result = HttpsClientUtil.doPost(API_URL + "/api/lapp/live/address/limited", paramMap);
        return JSONObject.parseObject(result);
    }

    /**
     * 删除设备
     * @param accessToken
     * @param deviceSerial
     * @return
     */
    public static YsApiResult offDeviceEncrypt(String accessToken, String deviceSerial, String validateCode){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("accessToken", accessToken);
        paramMap.put("deviceSerial", deviceSerial);
        paramMap.put("validateCode", validateCode);
        String result = HttpsClientUtil.doPost(API_URL + "/api/lapp/device/encrypt/off", paramMap);
        return JSONObject.parseObject(result, YsApiResult.class);
    }

    /**
     *
     * @param appKey
     * @param appSecret
     * @return
     */
    public static JSONObject refreshToken(String appKey, String appSecret){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("appKey", appKey);
        paramMap.put("appSecret", appSecret);
        String result = HttpsClientUtil.doPost(API_URL + "/api/lapp/token/get", paramMap);
        return JSONObject.parseObject(result);
    }

    public static void main(String[] args){
        String accessToken = "at.a7rccumg9dreo0hra60u8dyi7yfahx8j-4qoqytwzmv-048ymcl-xtcb0rfgn";
//        JSONObject result = YsDeviceApi.addDevice(accessToken, "202826112", "RYYLSO");

        System.out.println(YsDeviceApi.offDeviceEncrypt(accessToken, "202826112", "RYYLSO"));
    }
}
